package com.tebiecloud.mpopen.server;

import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.mpopen.client.constants.MpopenConstants;
import com.tebiecloud.mpopen.client.model.entity.MpopenAuthorizationInfo;
import com.tebiecloud.mpopen.server.service.MpopenAuthorizationInfoService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 第三方平台管理
 * @Author: tebie
 * @Date: 2019-09-07 16:02
 */
@Slf4j
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan(basePackages = "com.tebiecloud.mpopen.server.mapper")
public class MpopenApplication {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private MpopenAuthorizationInfoService mpopenAuthorizationInfoService;

    public static void main(String[] args) {
        SpringApplication.run(MpopenApplication.class, args);
    }

    @PostConstruct
    public void init() {
        log.info("xiaoc>> 初始化授权公众号");
        // 初始化授权公众号的 authorizer_refresh_token
        List<MpopenAuthorizationInfo> listMpAuthorizationInfo = mpopenAuthorizationInfoService.list();
        if(listMpAuthorizationInfo.size() > 0) {
            Map<String, String> mapMpAuthorizationInfo = new HashMap<String, String>();
            for(MpopenAuthorizationInfo item : listMpAuthorizationInfo) {
                mapMpAuthorizationInfo.put(item.getAuthorizerAppid(), item.getAuthorizerRefreshToken());
            }
            redisUtils.setMap(MpopenConstants.MP_AUTHORIZER_REFRESH_TOKEN, mapMpAuthorizationInfo);
        }
    }

}
